//
//  ANTableViewProfileCell.m
//  Ants
//
//  Created by ZHM on 16/3/17.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANTableViewProfileCell.h"


#import "ANProfileBaseItem.h"
#import "ANBadgeValue.h"
#import "ANProfieleHeader.h"
#import "ANPriceView.h"


@interface ANTableViewProfileCell ()

@property (nonatomic, strong) UIImageView *arrowView;
@property (nonatomic, strong) ANBadgeValue *badgeView;
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UIImageView *cheakView;
@property (nonatomic, strong) UILabel *labelView;
@property (nonatomic, strong) ANPriceView *priceView;

@end

@implementation ANTableViewProfileCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
#pragma mark --懒加载控件
- (ANPriceView *)priceView {
    if (!_priceView) {
        _priceView = [[ANPriceView alloc] init];
    }
    return _priceView;
}
- (UIImageView *)cheakView {
    if (!_cheakView) {
        _cheakView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_checkmark"]];
    }
    return _cheakView;
}
- (UIImageView *)arrowView {
    if (!_arrowView) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
    }
    return _arrowView;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
        _icon.bounds = CGRectMake(0, 0, 66, 66);
    }
    return _icon;
}
- (ANBadgeValue *)badgeView {
    if (!_badgeView) {
        _badgeView = [[ANBadgeValue alloc] init];
    }
    return _badgeView;
}
- (UILabel *)labelView {
    if (!_labelView) {
        _labelView = [[UILabel alloc] init];
        //_labelView.textColor = [UIColor redColor];
        //居中
        _labelView.textAlignment = NSTextAlignmentCenter;
    }
    return _labelView;
}
- (UISwitch *)switchView {
    if (!_switchView) {
        _switchView = [[UISwitch alloc] init];
        //监听事件
        [_switchView addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

#pragma mark --开关状态改变时候
- (void)valueChange:(UISwitch *)swith {
    //保持偏好设置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSLog(@"%@,%d,%d",_item.tilte,self.switchView.isOn,swith.on);
    [defaults setBool:swith.isOn forKey:_item.tilte];
    //同步
    [defaults synchronize];
}

- (void)setItem:(ANProfileBaseItem *)item {
    _item = item;
    self.textLabel.text = item.tilte;
    self.detailTextLabel.text = item.subTilte;
    self.imageView.image = item.image;
    //设置右边视图
    [self setUpRightView];
    
    //设置Lable
    if ([_item isKindOfClass:[ANlableItem class]]) {
        ANlableItem *item = (ANlableItem *)_item;
        self.labelView.text = item.text;
        [self addSubview:self.labelView];
    } else{
        [self.labelView removeFromSuperview];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.labelView.frame = self.bounds;
    _priceView.frame = CGRectMake(self.width - 200 - 8 - 27, 0, 200, 44);
    //ANLog(@"self.accessoryView  =  %@",NSStringFromCGRect(self.frame));
}
- (void)setUpRightView {
    if ([_item isKindOfClass:[ANArrowItem class]]) {
        self.accessoryView = self.arrowView;
    }else if ([_item isKindOfClass:[ANbadgeItem class]]){
        ANbadgeItem *badge = (ANbadgeItem *)_item;
        self.badgeView.badgeValue = badge.badgeValue;
        self.accessoryView = self.badgeView;
    }else if ([_item isKindOfClass:[ANCheakItem class]]){
        ANCheakItem *item = (ANCheakItem *)_item;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        if (item.cheak) {
            self.accessoryView = self.cheakView;
        } else {
            self.accessoryView = nil;
        }
    }else if ([_item isKindOfClass:[ANSwitchItem class]]){
        //HMSwitchItem *item = (HMSwitchItem *)_item;
        self.switchView.on = [[NSUserDefaults standardUserDefaults] boolForKey:_item.tilte];
        //开关CeLL不显示选中状态
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryView = self.switchView;
    }else if ([_item isKindOfClass:[ANIconItem class]]){
        ANIconItem *iconItem = (ANIconItem *)_item;
        NSURL *url = [NSURL URLWithString:iconItem.iconPath];
        ANLog(@"通过地址获得图片url=%@",url);
        if (url) {
            [self.icon sd_setImageWithURL:url];
        }else {
            self.icon.image = [UIImage imageNamed:@"default_portrait"];
        }
        
        self.accessoryView = self.icon;
    }else if ([_item isKindOfClass:[ANPriceItem class]]){
        self.accessoryView = self.arrowView;
        ANPriceItem *priceItem = (ANPriceItem *)_item;
        self.priceView.price = priceItem.price;
        self.priceView.originPrice = priceItem.originPrice;
        //self.accessoryView = self.priceView;
        [self addSubview: self.priceView];
        
    }else {
        self.accessoryView = nil;
    }
}
static NSString *ID = @"CELL";

+ (instancetype)cellWithTableView:(UITableView *)tabalView {
    ANTableViewProfileCell *cell = [tabalView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
